package com.jiecao.news.jiecaonews.backgroung;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.jiecao.news.jiecaonews.adapters.NewsAdapter;
import com.jiecao.news.jiecaonews.dao.NewsDao;
import com.jiecao.news.jiecaonews.dto.pb.PBAboutArticle.PBArticles;
import com.jiecao.news.jiecaonews.pojo.NewsListItem;
import com.jiecao.news.jiecaonews.util.PbUtils;
import com.jiecao.news.jiecaonews.util.constants.Constants;
import com.jiecao.news.jiecaonews.util.view.PullToRefreshListView;


public class GetListDataTask extends AsyncTask<Void, Void, String[]>{
	private static final String TAG = "GetListDataTask";
	private ArrayList<NewsListItem> mTempItems;//临时
	private ArrayList<NewsListItem> mItems;//新闻条目数据list
	private NewsAdapter mNewsAdapter;//新闻条目list adapter
	private PullToRefreshListView mPullRefreshListView;//下拉刷新view
	private String mTime;//传到服务器端的time
	private String mIsAfter;
	private NewsDao newsDao;
	private Context mcontext;
	public GetListDataTask(ArrayList<NewsListItem> items,NewsAdapter newsAdapter,PullToRefreshListView pullRefreshListView,String time, String isAfter,Context context){
		mItems = items;
		mNewsAdapter = newsAdapter;
		mPullRefreshListView =pullRefreshListView;
		mTempItems =new ArrayList<NewsListItem>();
		mTime = time;
		mIsAfter = isAfter;
		mcontext = context;
		newsDao = new NewsDao(mcontext);
	}
	@Override
	protected void onPostExecute(String[] result) {
		mTempItems.addAll(mItems);
		int count  = mTempItems.size();
		for(int i = Constants.LIST_PAGE_SIZE;i<count;i++){
			mTempItems.remove(mTempItems.size()-1);
		}
		for(int i =0;i<mTempItems.size();i++){
			System.out.println("doInBackground "+i+"     "+mTempItems.get(i).getTitle());
		}
		mItems.clear();
		mItems.addAll(mTempItems);
		mPullRefreshListView.onRefreshComplete();
		mNewsAdapter.notifyDataSetChanged();
		//下拉刷新，保存到数据库中
		newsDao.storeItems(mItems);
		super.onPostExecute(result);
	}

	@Override
	protected String[] doInBackground(Void... params) {
		SimpleDateFormat   formatter  =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss");  
		PBArticles pBArticles = PbUtils.getPBArticles(
				mTime, mIsAfter, Constants.LIST_PAGE_SIZE, null, 0);
		if (pBArticles == null) {
			return null;
		}
		for (int i =0;i< pBArticles.getArticlesCount();  i++) {
			NewsListItem listItem = new NewsListItem();
			String   date   =   formatter.format(new Date());
			listItem.setAppCreateTime(date);
			listItem.setIcon(pBArticles.getArticles(i).getIcon());
			listItem.setId(pBArticles.getArticles(i).getId());
			listItem.setIntro(pBArticles.getArticles(i).getIntro());
			listItem.setIsReaded(0);
			listItem.setPublishTime(pBArticles.getArticles(i).getPublishTime());
			listItem.setTitle(pBArticles.getArticles(i).getTitle());
			listItem.setShareNum(pBArticles.getArticles(i).getShareNum());
			mTempItems.add(listItem);
		}
		
		return null;
	}
}
